<?php declare(strict_types=1);

/**
 * @package Events (Light Portal)
 * @link https://custom.simplemachines.org/index.php?mod=4244
 * @author Bugo <bugo@dragomano.ru>
 * @copyright 2024-2025 Bugo
 * @license https://spdx.org/licenses/GPL-3.0-or-later.html GPL-3.0-or-later
 *
 * @category plugin
 * @version 06.11.25
 */

namespace LightPortal\Plugins\Events;

use Bugo\Compat\Actions\Calendar;
use Bugo\Compat\Lang;
use LightPortal\Enums\Tab;
use LightPortal\Plugins\Block;
use LightPortal\Plugins\Event;
use LightPortal\Plugins\PluginAttribute;
use LightPortal\UI\Fields\CheckboxField;
use LightPortal\UI\Fields\NumberField;
use LightPortal\UI\Fields\RangeField;
use LightPortal\Utils\Str;
use LightPortal\Utils\Traits\HasView;
use Ramsey\Collection\Map\NamedParameterMap;

if (! defined('LP_NAME'))
	die('No direct access...');

/**
 * Generated by PluginMaker
 */
#[PluginAttribute(icon: 'fas fa-calendar-check')]
class Events extends Block
{
	use HasView;

	public function prepareBlockParams(Event $e): void
	{
		$e->args->params = [
			'show_birthdays'  => false,
			'show_holidays'   => false,
			'show_events'     => true,
			'days_in_future'  => 7,
			'update_interval' => 600,
		];
	}

	public function validateBlockParams(Event $e): void
	{
		$e->args->params = [
			'show_birthdays'  => FILTER_VALIDATE_BOOLEAN,
			'show_holidays'   => FILTER_VALIDATE_BOOLEAN,
			'show_events'     => FILTER_VALIDATE_BOOLEAN,
			'days_in_future'  => FILTER_VALIDATE_INT,
			'update_interval' => FILTER_VALIDATE_INT,
		];
	}

	public function prepareBlockFields(Event $e): void
	{
		Lang::load('ManageCalendar');

		$options = $e->args->options;

		CheckboxField::make('show_birthdays', Lang::$txt['setting_cal_showbdays'])
			->setTab(Tab::CONTENT)
			->setValue($options['show_birthdays']);

		CheckboxField::make('show_holidays', Lang::$txt['setting_cal_showholidays'])
			->setTab(Tab::CONTENT)
			->setValue($options['show_holidays']);

		CheckboxField::make('show_events', Lang::$txt['setting_cal_showevents'])
			->setTab(Tab::CONTENT)
			->setValue($options['show_events']);

		RangeField::make('days_in_future', $this->txt['days_in_future'])
			->setAttribute('max', 60)
			->setValue($options['days_in_future']);

		NumberField::make('update_interval', $this->txt['update_interval'])
			->setAttribute('min', 0)
			->setValue($options['update_interval']);
	}

	public function changeIconSet(Event $e): void
	{
		$e->args->set['cake']  = 'fas fa-cake-candles';
		$e->args->set['event'] = 'fas fa-calendar-days';
	}

	public function getData(NamedParameterMap $parameters): array
	{
		$now = time();
		$todayDate = date('Y-m-d', $now);

		$futureDate = empty($parameters['days_in_future'])
			? $todayDate
			: date('Y-m-d', ($now + $parameters['days_in_future'] * 24 * 60 * 60));

		$options = [
			'show_birthdays' => $parameters['show_birthdays'],
			'show_holidays'  => $parameters['show_holidays'],
			'show_events'    => $parameters['show_events'],
		];

		return Calendar::getCalendarList($todayDate, $futureDate, $options);
	}

	public function prepareContent(Event $e): void
	{
		$parameters = $e->args->parameters;

		$data = $this->cache($this->name . '_addon_b' . $e->args->id)
			->setLifeTime(Str::typed('int', $parameters['update_interval']))
			->setFallback(fn() => $this->getData($parameters));

		echo $this->view(params: ['data' => $data]);
	}
}
